<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<h1>首页</h1>
<a id="exit" href="">退出</a>

<script>
    console.log(">>>首页<<<")
    window.addEventListener("load",function (e){
        e.preventDefault()
        let xhr = new XMLHttpRequest();
        xhr.open("post","http://localhost:8080/userinfo/1")
        let token=localStorage.getItem("token")
        xhr.setRequestHeader("Authorization",'Bearer '+token)
        xhr.onreadystatechange=function (){
            if(xhr.readyState===4){
                if(xhr.status===200){
                    console.log(xhr.responseText)
                    console.log(token)
                }else{
                    console.log("错误")
                    // console.log(xhr.status)
                    // console.log(xhr.responseText)
                }
            }
        }
        xhr.send()
    })

    document.getElementById("exit").addEventListener("click",function (e){
        e.preventDefault();
        let xhr = new XMLHttpRequest();
        xhr.open("post","http://localhost:8080/logout")
        let token=localStorage.getItem("token")
        xhr.setRequestHeader("Authorization",'Bearer '+token)
        xhr.onreadystatechange=function (){
            if(xhr.readyState===4){
                if(xhr.status===200){
                    console.log("成功")
                    console.log(token)
                    location.href="http://"+location.host+"/userLogin.html"
                }else{
                    console.log("错误")
                    // console.log(xhr.status)
                    // console.log(xhr.responseText)
                }
            }
        }
        xhr.send()
    })


</script>
</body>
</html>